﻿using KeaoBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace KeaoMDS.InfoManage
{
    public partial class GongYingShangBianJi : BaseForm
    {
        public GongYingShangBianJi()
        {
            InitializeComponent();
        }

        private DataGridViewRow m_CurrRow = null;

        public GongYingShangBianJi(DataGridViewRow row)
        {
            InitializeComponent();
            m_CurrRow = row;
        }


        private void menu_btn_baocun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_mingcheng.Text))
            {
                KeaoUtils.ShowInfoMessage("供应商名称不能为空，请检查重试。");
                return;
            }
            if (m_CurrRow == null)
            {
                string createCondition = KeaoUtils._DB.CreateAndCondition("mingcheng", DataValueCommonType.字符串, txt_mingcheng.Text, "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_gongyingshang", createCondition);
                if (Dt.Rows.Count > 0)
                {
                    KeaoUtils.ShowInfoMessage("供应商名称重复，请检查重试。");
                    return;
                }

                TableRow newRow = new TableRow("tbl_gongyingshang", Guid.NewGuid().ToString(), DBCtl.插入);
                newRow.DataCols.Add(new TableCol() { ColName="bianhao", ValueMember="", ValueType= DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName="mingcheng", ValueMember= txt_mingcheng.Text, ValueType= DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName="zhuangtai",ValueMember = chk_zhuangtai.Checked?"启用":"禁用", ValueType= DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName="zhandian", ValueMember=KeaoUtils.ConfigGetNodeAttr("客户","站点"), ValueType= DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "客户编号"), ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                if (KeaoUtils._DB.SaveTableRows(newRow)>0)
                {
                    DialogResult = DialogResult.OK;
                }

            }
            else
            {
                string createCondition = KeaoUtils._DB.CreateAndCondition("mingcheng", DataValueCommonType.字符串, txt_mingcheng.Text, "", SearchConditionType.等于);
                createCondition += KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", m_CurrRow.Cells["col_id"].Value), "", SearchConditionType.不等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_gongyingshang", createCondition);
                if (Dt.Rows.Count > 0)
                {
                    KeaoUtils.ShowInfoMessage("供应商名称重复，请检查重试。");
                    return;
                }

                TableRow editRow = new TableRow("tbl_gongyingshang", string.Format("{0}", m_CurrRow.Cells["col_id"].Value), DBCtl.修改);
                editRow.DataCols.Add(new TableCol() { ColName = "bianhao", ValueMember = "", ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "mingcheng", ValueMember = txt_mingcheng.Text, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "zhuangtai", ValueMember = chk_zhuangtai.Checked ? "启用" : "禁用", ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                if (KeaoUtils._DB.SaveTableRows(editRow) > 0)
                {
                    DialogResult = DialogResult.OK;
                }
            }
        }

        private void menu_btn_guanbi_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void GongYingShangBianJi_Load(object sender, EventArgs e)
        {
            if (m_CurrRow != null)
            {
                txt_mingcheng.Text = string.Format("{0}", m_CurrRow.Cells["col_mingcheng"].Value);
                if (m_CurrRow.Cells["col_zhuangtai"].Value.Equals("启用"))
                {
                    chk_zhuangtai.Checked = true;
                }
                else
                {
                    chk_zhuangtai.Checked = false;
                }
            }
        }
    }
}
